﻿using System;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Windows;

namespace Zorbo.UI
{
    public class Help : INotifyPropertyChanged
    {
        String helptext;
        Boolean isopen;

        UIElement control;


        public Boolean IsOpen {
            get { return isopen; }
            set {
                if (isopen != value) {
                    isopen = value;
                    RaisePropertyChanged(() => IsOpen);
                }
            }
        }

        public String Text {
            get { return helptext; }
            set {
                if (helptext != value) {
                    helptext = value;
                    RaisePropertyChanged(() => Text);
                }
            }
        }

        public UIElement Control {
            get { return control; }
            set {
                if (control != value) {
                    control = value;
                    RaisePropertyChanged(() => Control);
                }
            }
        }


        #region " INotifyPropertyChanged "

        protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> property) {
            var prop = ((MemberExpression)property.Body).Member as PropertyInfo;

            if (prop == null) return;
            OnPropertyChanged(prop.Name);
        }

        protected virtual void RaisePropertyChanged(string propName) {
            OnPropertyChanged(propName);
        }

        protected virtual void OnPropertyChanged(string propName) {
            var x = PropertyChanged;
            if (x != null) x(this, new PropertyChangedEventArgs(propName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


        public static void SetHelpText(DependencyObject target, string text) {
            target.SetValue(HelpTextProperty, text);
        }

        public static string GetHelpText(DependencyObject target) {
            return (string)target.GetValue(HelpTextProperty);
        }


        private static void HelpTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
            SetHelpText(obj, e.NewValue as string);
        }


        public static readonly DependencyProperty HelpTextProperty =
            DependencyProperty.RegisterAttached(
            "HelpText",
            typeof(String),
            typeof(Help),
            new PropertyMetadata(HelpTextChanged));
    }
}
